require(['grid','validator'], function () {
	var editDialog,funName, $search = $('#search'),$grid = $('#grid'),vrsnType="APUP0001",$addSet=$("#addSet");

	//顶部类型
	$(".element-select a").click(function(){
		vrsnType = $(this).attr('val');
		if(vrsnType=='APUP0001'){//核心板应用
			$grid.showHideCol('lineName','none');
			$grid.showHideCol('licPltNo','none');
			$grid.showHideCol('carModel','none');
			$grid.showHideCol('eqmtType','');
		}
		else if(vrsnType=='APUP0002'||vrsnType=='VERS0012'){//主板应用//CAN车身配置应用文件
			$grid.showHideCol('lineName','none');
			$grid.showHideCol('licPltNo','none');
			$grid.showHideCol('eqmtType','none');
			$grid.showHideCol('carModel','');
		}
		else if(vrsnType=='VERS0002'){//线路文件
			$grid.showHideCol('eqmtType','none');
			$grid.showHideCol('lineName','');
			$grid.showHideCol('licPltNo','');
			$grid.showHideCol('carModel','');
		}
		$(this).addClass("active");
		$(this).siblings().removeClass("active");
		$grid.grid('setGridParam', {
			datatype: "JSON",
			page: 1,
			postData:  {"vrsnName" : $("#vrsnName").val(), "vrsnType" : vrsnType}
		}).trigger("reloadGrid");
		$grid.jqGrid('resize');
	});

	//grid初始化
	function gridInit() {
		$grid.grid({
			url: "/upgrade/getFileSetPage.htm",
			colModel: [
				{name: "vrsnId", label: "vrsnId", key: true, hidden: true},
				{name: "lineName", label: "运营线路", hidden: true},
				{name: "licPltNo", label: "车牌号", hidden: true},
				{name: "vrsnName", label: "版本名称"},
				{name: "vrsnType", label: "版本类型", dict: "VERS0000"},
				{name: "eqmtType", label: "设备类型", dict: "EQFA0100"},
				{name: "carModel", label: "车辆型号"},
				{name: "appUpgrade", label: "应用升级项", dict: "APUP0000"},
				{name: "ftpUpgradeAdr", label: "FTP升级地址"},
				{name: "remark", label: "备注"},
				{name: "operate", label: '操作', title: false,align:'center',
					formatter: 'operArea',
					formatoptions: [{
						text: '删除',
						css:'operate-delete',
						click: function (data) {
							$.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
								if (r) {
                                    $mask = $.createMask();  //遮罩
									$.ajaxExt({
										url : "/upgrade/delUpgrade.htm",
										data : {'vrsnId' : data.vrsnId,"vrsnType":vrsnType}
									}).done(function(){
                                        $mask.remove();
										reload();
									});
								}
							});
						}
					}]
				},
			],
			multiselect: false,
			height:$(window).height()-180
		});
	}

	/**
	 * 新增按钮
	 * @param option {object} 对话框配置
	 * @returns {jQuery}
	 */
	$addSet.click(function() {
		var nodes=commonFun.getLeftTreeSelectedNodes();
		if(vrsnType=="APUP0001"){
			upgradeDialog('新增核心板应用','./upgrade/setting/UpgradeFileSetCore.htm')
		}else if(vrsnType=="APUP0002"){
			upgradeDialog('新增主板应用','./upgrade/setting/UpgradeFileSetMoth.htm')
		}else if(vrsnType=="VERS0002"){
			upgradeDialog('新增线路文件','./upgrade/setting/UpgradeFileSetLine.htm')
		}else if(vrsnType=="VERS0012"){
			upgradeDialog('新增CAN车身配置应用文件','./upgrade/setting/UpgradeFileSetCAN.htm')
		}
	});

	/**
	 * 获取表单对话框，如果没有就先创建
	 * @param option {object} 对话框配置
	 * @returns {jQuery}
	 */
	function upgradeDialog(title,url) {
		editDialog = $('<div>').dialog({
			title:title,
			url: url,
			height: 500,
			width: 780,
			autoOpen: true,
			buttons: [
				{
					text: '保存',
					css: 'mr10',
					click: function () {
						var _t = this;
						$(_t).dialog('exec', 'add',function(){
							reload(true);
						});
					}
				},
				{
					text: '关闭',
					css: 'btn-cancel',
					click: function () {
						$(this).dialog('close');
					}
				}
			]
		});
		return editDialog;
	}

	//查询grid
	$search.on("click", function () {
		$grid.grid('setGridParam', {
			datatype: "JSON",
			page: 1,
			postData:  {"vrsnName" : $("#vrsnName").val(), "vrsnType" : vrsnType}
		}).trigger("reloadGrid");
	});

	//新增修改后执行
	function reload(close){
		$grid.grid('setGridParam', {
			datatype: "JSON",
			page: 1,
			postData:  {"vrsnName" : $("#vrsnName").val(), "vrsnType" : vrsnType}
		}).trigger("reloadGrid");
		close && editDialog.dialog('close');
	};
	gridInit();

});